package com.bmadmin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bmadmin.domain.AddressUser;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.PluginZtreeDataPOJO;
import com.bmadmin.domain.SysDepartmentsPOJO;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.mapper.AddressDao;
import com.bmadmin.service.AddressService;
@Service
@Transactional
public class AddressServiceImpl implements AddressService {
	@Autowired
	AddressDao addressDao;
	
	public NormalResult getAddressTreeList(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();
        List<PluginZtreeDataPOJO> pluginZtreeDataPOJOS = new ArrayList<PluginZtreeDataPOJO>();
        List<SysDepartmentsPOJO> sysDepartmentsPOJOS = addressDao.getAddressTreeList(sysDepartmentsPOJO);

        for (SysDepartmentsPOJO sysDepartmentsPOJOItem : sysDepartmentsPOJOS) {
            PluginZtreeDataPOJO forAddDepartment = new PluginZtreeDataPOJO();

            forAddDepartment.setId(sysDepartmentsPOJOItem.getD_id());
            forAddDepartment.setPart_id(sysDepartmentsPOJOItem.getD_part_id());
            forAddDepartment.setName(sysDepartmentsPOJOItem.getD_id() + " " + sysDepartmentsPOJOItem.getD_name());
            forAddDepartment.setOpen(true);
            forAddDepartment.setClick("getLineClick('" + sysDepartmentsPOJOItem.getD_id() + "')");

            pluginZtreeDataPOJOS.add(forAddDepartment);
        }

        for (PluginZtreeDataPOJO sysDepartmentsPOJOItemOne : pluginZtreeDataPOJOS) {
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();
            for (PluginZtreeDataPOJO sysDepartmentsPOJOItemTwo : pluginZtreeDataPOJOS) {
                if (sysDepartmentsPOJOItemOne.getId().equals(sysDepartmentsPOJOItemTwo.getPart_id())) {
                    forAddChildren.add(sysDepartmentsPOJOItemTwo);
                }
            }
            sysDepartmentsPOJOItemOne.setChildren(forAddChildren);
        }
        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();
        for (PluginZtreeDataPOJO sysDepartmentsPOJOItem : pluginZtreeDataPOJOS) {
            if ("00".equals(sysDepartmentsPOJOItem.getPart_id())) {
                resultForLevelOne.add(sysDepartmentsPOJOItem);
            }
        }
        result.setCode("0000");
        result.setMsg("查询成功");
        result.setObject(resultForLevelOne);
        return result;
	}
	
	public int getNextDepartId(String d_part_id){
		return addressDao.getNextDepartId(d_part_id);
	}

	public int getNextDId(String d_part_id){
		return addressDao.getNextDId(d_part_id);

	}

	public NormalResult insertDepartment(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();
        SysDepartmentsPOJO sysDepartmentsPOJOForQueryID = new SysDepartmentsPOJO();
        sysDepartmentsPOJOForQueryID.setD_id(sysDepartmentsPOJO.getD_id());
        int resultForInsert = addressDao.insertSelective(sysDepartmentsPOJO);
        if (resultForInsert == 1) {
            result.setCode("0000");
            result.setMsg("新增成功");
        } else {
            result.setCode("9999");
            result.setMsg("新增分类异常，请联系系统管理员");
        }

        return result;
	}
	
    @Override
    public NormalResult getDepartmentById(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();
        List<SysDepartmentsPOJO> sysDepartmentsPOJOS = addressDao.getAddressTreeList(sysDepartmentsPOJO);
        if (sysDepartmentsPOJOS.size() > 0) {
            result.setCode("0000");
            result.setMsg("查询成功");
            result.setData(sysDepartmentsPOJOS.get(0));
        } else {
            result.setCode("9999");
            result.setMsg("查询数据为空");
        }

        return result;
    }
    
    @Override
    public NormalResult updateDepartment(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();

        int resultForUpdate = addressDao.updateDepartment(sysDepartmentsPOJO);
        if (resultForUpdate == 1) {
            result.setCode("0000");
            result.setMsg("修改成功");
        } else {
            result.setCode("9999");
            result.setMsg("修改分类异常，请联系系统管理员");
        }

        return result;
    }
    
    @Override
    public NormalResult delDepartment(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();
        SysDepartmentsPOJO sysDepartmentsPOJOForQueryChildren = new SysDepartmentsPOJO();
        sysDepartmentsPOJOForQueryChildren.setD_part_id(sysDepartmentsPOJO.getD_id());
        List<SysDepartmentsPOJO> resultByQueryChildren = addressDao.getAddressTreeList(sysDepartmentsPOJOForQueryChildren);
        if (resultByQueryChildren.size() > 0) {
            result.setCode("9999");
            result.setMsg("该部门存在下级,不得进行删除");
            return result;
        }
        AddressUser sysUsersPOJOForQueryDepartment = new AddressUser();
        sysUsersPOJOForQueryDepartment.setD_id(sysDepartmentsPOJO.getD_id());
        List<AddressUser> sysUsersPOJOSForQueryDepartment = addressDao.getUserList(sysUsersPOJOForQueryDepartment);
        if (sysUsersPOJOSForQueryDepartment.size() > 0) {
            result.setCode("9999");
            result.setMsg("该分类下存在用户，不得进行删除");
            return result;
        }

        int resultForDelete = addressDao.delDepartment(sysDepartmentsPOJO);
        if (resultForDelete == 1) {
            result.setCode("0000");
            result.setMsg("删除成功");
        } else {
            result.setCode("9999");
            result.setMsg("删除分类异常，请联系系统管理员");
        }
        return result;
    }
	
	public LayUiDataTablePOJO getUserList(AddressUser addressUser){
		LayUiDataTablePOJO result = new LayUiDataTablePOJO();

        List<AddressUser> sysUsersPOJOS = addressDao.getUserList(addressUser);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setData(sysUsersPOJOS);
        result.setCount(sysUsersPOJOS.size());


        return result;
	}
	
	public NormalResult insertUser(AddressUser addressUser){
		NormalResult result = new NormalResult();
		int insertResult = addressDao.insertUser(addressUser);
        if(insertResult==1){
            result.setCode("0000");
            result.setMsg("人员新增成功");
        }else{
            result.setCode("9999");
            result.setMsg("人员新增成功");
        }
        return result;
	}

	public NormalResult updateUser(AddressUser addressUser){
		NormalResult result = new NormalResult();
		int updateResult = addressDao.updateUser(addressUser);
        if(updateResult==1){
            result.setCode("0000");
            result.setMsg("人员修改成功");
        }else{
            result.setCode("9999");
            result.setMsg("人员修改失败");
        }
        return result;
	}

	public NormalResult deleteUser(String u_id){
		NormalResult result = new NormalResult();
        int deleteResult = addressDao.deleteUser(u_id);
        if(deleteResult==1){
            result.setCode("0000");
            result.setMsg("用户删除成功");
        }else{
            result.setCode("9999");
            result.setMsg("用户删除失败");
        }
        return result;
	}
}
